package com.open.capacity.dao;

import com.open.capacity.model.MessageModel;
import com.open.capacity.model.OrderAndStation;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author: y_022
 * @date: 9:30-2021/6/25
 * @description:
 */
public interface OrderDao {
    void addOrder(@Param("orderModel") OrderAndStation orderModel);

    List<OrderAndStation> findOrderByUserId(@Param("orderModel") OrderAndStation orderModel);

    List<OrderAndStation> findPaidOrder(@Param("orderModel") OrderAndStation orderModel);

    List<OrderAndStation> findUnpaidOrder(@Param("orderModel") OrderAndStation orderModel);

    void cancelOrder(@Param("orderModel") OrderAndStation orderModel);

    void leaseRenewal(@Param("orderModel") OrderAndStation orderModel);

    void addOrderToBusiness(@Param("orderAndStation") OrderAndStation orderAndStation);


    @Select("  SELECT o.*,s.statid,s.stname,s.busiid,s.addrid,s.conten,\n" +
            "               s.topimg,s.sttype,s.addres,s.starea,s.actiid\n" +
            "        FROM `order` o INNER JOIN station s on o.ordeid = #{orderAndStation.ordeid} AND s.statid= o.statid And o.enable =1 ")
    OrderAndStation findStationByOrdeid(@Param("orderAndStation") OrderAndStation orderAndStation);

    @Update("update `order` set enable = 1 where orcode = #{orcode}")
    void orderTimeEnd(String orcode);

    @Select(" SELECT o.*,s.statid,s.stname,s.busiid,s.addrid,s.conten,\n" +
            "               s.topimg,s.sttype,s.addres,s.starea,s.actiid\n" +
            "        FROM `order` o INNER JOIN station s on o.userid =#{orderAndStation.userid} AND s.statid= o.statid And o.enable =2  order by o.crdate desc")
    List<OrderAndStation> findUsingOrder(@Param("orderAndStation") OrderAndStation orderAndStation);

    @Update("update `order` set enable = 3  where orcode =#{orcode} ") //到期未支付设置为无效订单
    void InvalidationOrder(@Param("orcode") String orcode);

    @Update("update `order` set enable = 2  where orcode = #{orcode} ")//设置为正在使用中
    void usingPaid(String orcode);

    @Select(" SELECT o.*,s.statid,s.stname,s.busiid,s.addrid,s.conten,\n" +
            "               s.topimg,s.sttype,s.addres,s.starea,s.actiid\n" +
            "        FROM `order` o INNER JOIN station s on o.userid =#{orderModel.userid} AND s.statid= o.statid And o.enable =3  order by o.crdate desc")
    List<OrderAndStation> findInvalidationOrder(Long userid);

    @Insert("insert into user_message(userid,conten,metype) value(#{messageModel.userid},#{messageModel.conten},#{messageModel.metype})")
    void sendMessageForOrder(@Param("messageModel") MessageModel messageModel);


    OrderAndStation findOrderByOrcode(@Param("orcode") String orcode);

    void addUsageTimeRecord(@Param("time") String time, @Param("orcode") String orcode, @Param("statid") Long statid);

    @Select("select *\n" +
            "from station where statid = #{statid}")
    OrderAndStation findStationByStatid(Long statid);

}
